package com.mlick.a.other;// package cn.other;
//
// import org.bouncycastle.jce.provider.BouncyCastleProvider;
// import org.bouncycastle.util.encoders.Base64;
// import org.junit.Test;
// import sun.misc.BASE64Decoder;
// import sun.misc.BASE64Encoder;
//
// import javax.crypto.Cipher;
// import java.io.BufferedWriter;
// import java.io.FileWriter;
// import java.io.IOException;
// import java.nio.charset.Charset;
// import java.nio.charset.StandardCharsets;
// import java.nio.file.Files;
// import java.nio.file.Paths;
// import java.security.*;
// import java.security.interfaces.RSAPrivateKey;
// import java.security.interfaces.RSAPublicKey;
// import java.security.spec.InvalidKeySpecException;
// import java.security.spec.PKCS8EncodedKeySpec;
// import java.security.spec.X509EncodedKeySpec;
//
// /**
//  * @author lixiangxin
//  * @date 2019/5/6 18:34
//  **/
// public class RSATest {
//
//   //    public static final String RSA_ALGORITHM = "RSA";
//   public static final String RSA_ALGORITHM = "RSA/ECB/OAEPWithSHA1AndMGF1Padding";
//   public static final Charset UTF8 = Charset.forName("UTF-8");
//
//
//   public static void generateRSAKeystore() throws Exception {
// //    // generate public and private keys
//     KeyPair keyPair = buildKeyPair();
//     PublicKey publicKey = keyPair.getPublic();
//     PrivateKey privateKey = keyPair.getPrivate();
// //
//     savePrivateKey(privateKey);
//
//     savePublicKey(publicKey);
//   }
//
//   public static void main(String[] args) throws Exception {
//
//     Security.addProvider(new BouncyCastleProvider());
//
// //    generateRSAKeystore();
//
//     // encrypt the message
// //    byte[] encrypted = encrypt(loadPublicKey(), "my name is ecofast小0胡！!");
//     byte[] encrypted = encrypt(loadPublicKey(), "528910$9629a25f-1f86-4c6717bac909-8920-4a3e$17bac909-8920-4a3e-a037-6f36eaa674c5$47404b5c0d28dbf41d2b749b28ef8a73c0dc2943ffa3579deac4447dfb2dae9b$2a44cfd3523f9710a04bf336dcf3f281c29238873b95406c6d9e2863d892b443");
//     String base64Encode = base64Encode(encrypted);
//     System.out.println(base64Encode.length());
//     System.out.println(base64Encode.replaceAll("\n|\r", ""));  // <<encrypted message>>
//
//     // decrypt the message
//     byte[] secret = decrypt(loadPrivateKey(), encrypted);
//     System.out.println(new String(secret, UTF8));     // This is a secret message
//   }
//
//
//   @Test
//   public void t1() throws Exception {
//     Security.addProvider(new BouncyCastleProvider());
//
// //    String encrypted = "QpjCNpt70CqDg5/2zM5EFYu0C/pk7qLnZUsfIQtMztaf93yNdNgr3NbZR7CbILqGo5YNk+JE+DaVnkcAcPSIg2/z94SyY4ANd2LLq7vVnsQTKeGKM6NrrDCSg+wiKo4EwJFbT3QyM/5+YY5MZJJnBY82um2j4xvKrP2f41V7rLwt5Jv4nfB132FyMl0RwvFTTCm6/Qn5BC/7AIjXDpYQE6bS/GuCqNBwVh+qzoyA1KXgxuqlX/pmPpI1Z1daufDDyN9s/vvNP3M3SY4NbHH58Lsa0SjDYK3KU/qGqkPqQfX09oHrVB9Qc+nZdVMN7iKF/HK9wmG9srWSfOwrz9uK5g==";
// //    String encrypted = "nBha870xK07BDFXKMwS5eo1gRWgKNdQGYvg3CBjSxB4Vh+OZ5qFJOJvLsiNQSyEPG8cc46UnIZvtVEAlfqDkTPBAwVNW7vED3ymYn8U6NegYcxj4jUnH4WRkH7SzYTpFbRF1gjhHOqgzXBImxtFAzB+t5HPgxQ0JjXQxRNkvPEjUojqigRPl0z1FLitLSnqkk6qrDaJS8azh6AqygnjwnUsbVxKE+roa1Kdgf2jIaV045JZQ5L2MT7SvHi6rO//Zb/HyJo73ctlwHCy22EISG5fzkbZfD0IjGgM2QQgC1Eo3Buv5Cm6WstWIt8CWzbU9wtYJnD5g2SXSpQk0ZfSLGQ==";
// //    String encrypted = "A4STluBcSNalp0vWwLVg0ZzBZYkgDP5Ncg0vlg1Mfk-3FWj2MUHvUzgOQDL7sm-PUDFzQDQbK01BG9wwPVUVw_QNGpOvLfQaKqtTjrZvV01KN458QWJXRLwi4bBVvHlL_uq77WvpUVDtjVSmIZ2PjdJtcdmIbpfIBvRFXcsZpTR-TSsDT1XQlNLLpT_XQs-tNNB8uZKPjs5eOut0fSQau49K9HpN2-dbdGbFOxtIF6Iq_W-JdfQf4wc0css8wtw_wat4PIWv9tJmaV-G1Ax3jLBpgM3eWw0CjnXd6Td-wtwVbvJ-MmJarisY6zUNq0-QrRjtwLcLbTKzo-6JfTgxDw";
// //    String encrypted = "g+fnHuham2c/+5YnXA+ZP/ilGoELLsxoieydB5eVQZF+F2RWsK2S6u3XButZ+oqS9LRd8ycXhZHP\n" +
// //        "TJ7aqhMARCcMAhVPw6IiPKLNSrcy1Mq3DLBKP928JbFKqyYD9i2XvL6kH9uCybcWzF0GY/YexQba\n" +
// //        "skgMSeCPyRBkNXPR6rLZXm2oCPRLzJOE/02EY2TpKiyNwSAmUzxlneqldvwpMA6Fg+9k4KSPwc3Q\n" +
// //        "eTrS/dxUTgOG9zts83n5taRWXyXp9f+IGDPfUJLzooKI4S2Z+VwUXf7C0MoW6/W6rFKNbcJVwxXg\n" +
// //        "tJGGfNqyCSg0cuKuXfOiSNHAHK+73HRlJn5UjQ==";
//
//     String encrypted = "EBzOmMnfwYCYPfkTb3JeNIuoX63wpXeRHNMibWcBHL1HjMvZXa2ynwVp4WpwP6hi+w1jpC36dgeOjI1fEHRLPaSgACj4m60mwdUMOHUthzfsOggT+OtNd40iqOxv0E40sm/K5T7xKFwzR7jC6kMK1L2cnjswH0nxXlZWcZZ1mBs8WTn7alBFDIP62JXjfDVnsiY18TULFzTvpK1xKmpw9mHtJLnGcP8+Cn+voAs6nY0bZYWJ2LbIIdLsziNBMfGEohQOdGymiKhZ16MAx3TVg/sqQXsPOPjWz9Ga/PPZY378/UpVQkflJneizQAglpQFptZuqON6PeS0Zw/2+fGQPw==";
//
//     byte[] secret = decrypt(loadPrivateKey(), Base64.decode(encrypted));
//     System.out.println(new String(secret, UTF8));
//
//   }
//
//   public static KeyPair buildKeyPair() throws NoSuchAlgorithmException {
//     final int keySize = 2048;
//     KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//     keyPairGenerator.initialize(keySize);
//     return keyPairGenerator.genKeyPair();
//   }
//
//   public static byte[] encrypt(PublicKey publicKey, String encrypted) throws Exception {
//     Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
//     cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//
//     return cipher.doFinal(encrypted.getBytes(UTF8));
//   }
//
//   public static byte[] decrypt(PrivateKey privateKey, byte[] message) throws Exception {
//     Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
//     cipher.init(Cipher.DECRYPT_MODE, privateKey);
//
//     return cipher.doFinal(message);
//   }
//
//   /**
//    * 从字符串中加载公钥
//    */
//   public static RSAPublicKey loadPublicKey() throws Exception {
//     try {
// //      StringBuilder publicKeyStrB = new StringBuilder();
// //      Files.readAllLines(Paths.get("publicKey-" + RSA_ALGORITHM.replaceAll("/", "_") + ".keystore")).forEach(s -> {
// //        publicKeyStrB.append(s);
// //      });
// //      String publicKeyStr = publicKeyStrB.toString();
//     String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAokFjy0wLMKH0/39hxPN6JYRkMDXzvVIG\n" +
//         "Qh55Keo2LIsP/jRU/yZHT/Vkg34yU9koNjSaacPvooXEoI5eFGuRrsBMrotZ5xfejCrTbGvZqjhn\n" +
//         "MPBheDmxfflIZzRrF/zoQvF0nIbmGNkxEfROHtDDkgNuGRdthXrNavCgfM2z3LNF83UT9CGpxJWB\n" +
//         "eKK3pXfYLsQ4f8uyrQRcy2BhKfJ/PKai1mocXYqr07JfQ0XZM4xIzuQ7E4ybNk5IFreDuuhF63wX\n" +
//         "Ai1uonGzqjEYcbC1xT2boNiZORoOQWpAHhSbIRpljmW/uHBvoKZ573PQbbxE62hXv1Z1iVky0dtA\n" +
//         "V65dXwIDAQAB";
//
//       byte[] buffer = base64Decode(publicKeyStr);
//       KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//       X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
//       return (RSAPublicKey) keyFactory.generatePublic(keySpec);
//     } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
//       throw new RuntimeException(e);
//     }
//   }
//
//   public static RSAPrivateKey loadPrivateKey() throws Exception {
//     try {
// //      StringBuilder privateKeyStrB = new StringBuilder();
// //      Files.readAllLines(Paths.get("privateKey-" + RSA_ALGORITHM.replaceAll("/", "_") + ".keystore")).forEach(s -> {
// //        privateKeyStrB.append(s);
// //      });
// //
// //      String privateKeyStr = privateKeyStrB.toString();
//     String privateKeyStr = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCiQWPLTAswofT/f2HE83olhGQw\n" +
//         "NfO9UgZCHnkp6jYsiw/+NFT/JkdP9WSDfjJT2Sg2NJppw++ihcSgjl4Ua5GuwEyui1nnF96MKtNs\n" +
//         "a9mqOGcw8GF4ObF9+UhnNGsX/OhC8XSchuYY2TER9E4e0MOSA24ZF22Fes1q8KB8zbPcs0XzdRP0\n" +
//         "IanElYF4oreld9guxDh/y7KtBFzLYGEp8n88pqLWahxdiqvTsl9DRdkzjEjO5DsTjJs2TkgWt4O6\n" +
//         "6EXrfBcCLW6icbOqMRhxsLXFPZug2Jk5Gg5BakAeFJshGmWOZb+4cG+gpnnvc9BtvETraFe/VnWJ\n" +
//         "WTLR20BXrl1fAgMBAAECggEAFiQUKRa8ZCuFkiPgxI5w+ZOiVE3Yb9o0jms4H3cdY2CqkwazHoor\n" +
//         "qwaSa7sZxQVaDmOJOquJFMVqOnqS7fhrzcvh6uOLJhTEwp3Jcf8ZmbfOpQf4VP6ScN1GewbVIlXZ\n" +
//         "n+DxB7cEyRlCL/AFhQD53v/m0ze2M0O5qkEhtUYgqBn6kmR1ZzZ+Oo8gQrpuU1LGwsZBqyB74DsM\n" +
//         "kHjcrvBuywwGmP8fkKyv8zoZU4udFEnLZzBQGksGXy/HUd47To3SfvivhNnPYuBpVDgn6JIg9+ZW\n" +
//         "SbK4PS09SU1PRNZpbcXC4iiUP4gXs0iwbl3ECllQz/eGsgzAJfQZVk+6SxvHOQKBgQDkch5G44Oh\n" +
//         "P8r8p8LN3F1Mmp6W+U8sdNonq3MwFGe6uNHGLMghTYEYf8OVoR8JWdvohX6vxzg/7X/KfpwMXi6j\n" +
//         "tYfqmmVYr+iaFi3tdXk4urbxw4jqIXqw79xOSp5VzPvrQwhSc+DaRNGP9ptSL48bX/3gXX5yzIpY\n" +
//         "l5714EbcBQKBgQC103cnasqXRhcvB1u3z+KUWd1PRcmy/4uZIM09r2bvlT26bPRDfdapOqBPAss4\n" +
//         "ZPTxkBTs+wAI5LoJjjczGJBEnEke4PHmsf2D0aE0+8H23xePyRKZMlEPU5k08vzyglDIcekb2mDU\n" +
//         "ua8xgZXP6KlAEKFhUurBQBQeUP258JM1EwKBgEBfE77/7yBp3GXzU8Y0WSnzy8pY+AFb7/kLq8qZ\n" +
//         "hV0tJRdFmSMhNsZTgiZQn3FAG+Jac4dOiMAiyUajBI/LnjLNPT5TQUmLT+zTYR+vYXglzNU1Nwha\n" +
//         "ZLsk65w4FIOy3i291sN9GZgHmlGJuNygwB+DngMx9SaeEkHCf61jBDSNAoGAYH/Qt0zIJK0AVOdU\n" +
//         "2CMun4lqGU5yk9toXWa5Rsm2meTdE/oLUV6R9NK2jfhvnoiGL0ZGtNcp14585eFijm2085T2gbrk\n" +
//         "54kDgVhadGJBOSKBwL/4bPH1b2rw3tcvRA3BjgBWtX+a8Itz3ZRmzCcw3F13KM325YV6p/RYFL0H\n" +
//         "g50CgYEAri/q4gsuWFZcW5eymokNxrtt70N7d47E1XslC9b+fIWSGnBTP5IrPsciv+CRnkq3G+fG\n" +
//         "FWCNfaNO7dc/9OTbCWDlR3ElMBx8ja/sLHSO0ya2F5oSMAyAO8vt3rzhsAJUvfJ+pDLrbjBSvfNG\n" +
//         "e4ccc2gfrI8y78gdQLMXRzKWsxk=";
//
//         byte[] buffer = base64Decode(privateKeyStr);
//         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
//         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//         return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
//       } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
//         throw new RuntimeException(e);
//       }
//     }
//
//     public static void savePublicKey (PublicKey publicKey) throws IOException {
//       // 得到公钥字符串
//       String publicKeyString = base64Encode(publicKey.getEncoded());
//       System.out.println("publicKeyString=" + publicKeyString);
//       FileWriter fw = new FileWriter("publicKey-" + RSA_ALGORITHM.replaceAll("/", "_") + ".keystore");
//       BufferedWriter bw = new BufferedWriter(fw);
//       bw.write(publicKeyString);
//       bw.close();
//     }
//
//     public static void savePrivateKey (PrivateKey privateKey) throws IOException {
//       // 得到私钥字符串
//       String privateKeyString = base64Encode(privateKey.getEncoded());
//       System.out.println("privateKeyString=" + privateKeyString);
//
//       BufferedWriter bw = new BufferedWriter(new FileWriter("privateKey-" + RSA_ALGORITHM.replaceAll("/", "_") + ".keystore"));
//       bw.write(privateKeyString);
//       bw.close();
//     }
//
//     // public static String base64Encode ( byte[] data){
//     //   return new BASE64Encoder().encode(data);
//     // }
//     //
//     // public static byte[] base64Decode (String data) throws IOException {
//     //   return new BASE64Decoder().decodeBuffer(data);
//     // }
//
//   }
